package com.jcfriends.main.payment;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jcfriends.common.manager.SessionManager;
import com.jcfriends.common.util.DateUtils;
import com.jcfriends.main.payment.pojo.Payment;
import com.jcfriends.main.payment.pojo.PaymentInfo;
import com.jcfriends.main.payment.service.PaymentService;

@Controller
public class PaymentController {
	
	PaymentController(){System.out.println("\t > PaymentController()");}
	
	@Autowired
	private PaymentService paymentService;
		
	private String DEFAULT_PATH = "/m/main/payment/";
	
	/**
	 * 총무 시퀀스
	 */
	@Value("#{jcfriendProps['jc.manager.seq']}")
	private int CHECKER_SEQ;
	
	/**
	 * 월 회비금액
	 */
	@Value("#{jcfriendProps['jc.pay.amount']}")
	private int PAY_AMOUNT;
	
	
	/**
	 * 납부내역 조회
	 * @param map
	 * @param paymentInfo
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET, value={"/main/payment"})
	public String getPaymentList(ModelMap map, PaymentInfo paymentInfo){
		/*
		NetworkInterface iface = null;
		try {
			for(Enumeration ifaces =      NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
				iface = (NetworkInterface)ifaces.nextElement();
				System.out.println("Interface:"+ iface.getDisplayName());
				InetAddress ia = null;
				for(Enumeration ips =    iface.getInetAddresses();ips.hasMoreElements();){
					ia = (InetAddress)ips.nextElement();
					System.out.println(ia.getCanonicalHostName()+" "+    ia.getHostAddress());
				}
			}
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/ 
		//조회날짜 초기 세팅.
		if(paymentInfo.getStandardYyyy() == 0 || paymentInfo.getStandardMm() == 0){
			
			paymentInfo.setStandardYyyy(DateUtils.getYear());
			paymentInfo.setStandardMm(DateUtils.getMonth());
			
		}
		
		
		//총무
		if( isChecker() ){
			ArrayList<PaymentInfo> paymentList = paymentService.getPaymentList(paymentInfo);
			
			map.addAttribute("CHECKER_SEQ", CHECKER_SEQ);
			map.addAttribute("PAY_AMOUNT", PAY_AMOUNT);
			
			map.addAttribute("paymentList", paymentList);
			
			return DEFAULT_PATH + "payment_list_checker";
			
		}
		//납부자
		else{
			ArrayList<PaymentInfo> paymentList = paymentService.getPaymentList(paymentInfo);
			
			map.addAttribute("CHECKER_SEQ", CHECKER_SEQ);
			map.addAttribute("PAY_AMOUNT", PAY_AMOUNT);
			
			map.addAttribute("paymentList", paymentList);
			
			return DEFAULT_PATH + "payment_list";
			
		}
		
		
//		return DEFAULT_PATH + "payment_list";
	}
	
	/**
	 * 납부내역 등록
	 * @param paymentInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method=RequestMethod.POST, value={"/main/payment"})
	public String postPayment(PaymentInfo paymentInfo) throws Exception{
		
		if(CHECKER_SEQ == SessionManager.getSessionUserSeq()){
			paymentInfo.setChecker(CHECKER_SEQ);
		}
		
		paymentService.postPayment(paymentInfo);
		
		return "redirect:/main/payment";
	}
	
	/**
	 * 납부내역 수정
	 * @param paymentInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method=RequestMethod.PUT, value={"/main/payment"})
	public String putPayment(PaymentInfo paymentInfo) throws Exception{
		
		paymentInfo.setChecker(SessionManager.getSessionUserSeq());
		paymentInfo.setPayAmount(PAY_AMOUNT);
		
		paymentService.putPayment(paymentInfo);
		
		return "redirect:/main/payment";
	}
	
	@RequestMapping("/main/payment/detail")
	public @ResponseBody Payment ajaxTest(PaymentInfo paymentInfo) throws Exception{
		
		return paymentService.getPayment(paymentInfo);
	}
	
	
	
	
	
	
	
	
	
	
	/**
	 * 총무인지 판단
	 * @param userSeq
	 * @return
	 */
	private boolean isChecker(){
		try {
			int userSeq = SessionManager.getSessionUserSeq();
			if( userSeq == 0 || this.CHECKER_SEQ != userSeq ) return false;
		} catch (Exception e) {
			return false;
		}
		
		return true;
	}
}
